package com.an.blog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.an.blog.admin.domain.bo.BlogSocialUserBo;
import com.an.blog.admin.domain.vo.BlogSocialUserVo;
import com.an.blog.mapper.BlogSocialUserMapper;
import com.an.blog.service.IBlogSocialUserService;
import com.an.blog.web.domain.BlogSocialUser;
import com.an.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 社会化用户
 * Service业务层处理
 *
 * @author Ann
 * @date 2023-08-22
 */
@RequiredArgsConstructor
@Service
public class BlogSocialUserServiceImpl implements IBlogSocialUserService {

    private final BlogSocialUserMapper baseMapper;


    /**
     * 查询社会化用户
     * 列表
     */
    @Override
    public List<BlogSocialUserVo> queryList(BlogSocialUserBo bo) {
        LambdaQueryWrapper<BlogSocialUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BlogSocialUser> buildQueryWrapper(BlogSocialUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BlogSocialUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getUuid()), BlogSocialUser::getUuid, bo.getUuid());
        lqw.eq(StringUtils.isNotBlank(bo.getSource()), BlogSocialUser::getSource, bo.getSource());
        lqw.eq(StringUtils.isNotBlank(bo.getAccessToken()), BlogSocialUser::getAccessToken, bo.getAccessToken());
        lqw.eq(StringUtils.isNotBlank(bo.getRefreshToken()), BlogSocialUser::getRefreshToken, bo.getRefreshToken());
        lqw.eq(StringUtils.isNotBlank(bo.getOpenId()), BlogSocialUser::getOpenId, bo.getOpenId());
        lqw.eq(StringUtils.isNotBlank(bo.getUid()), BlogSocialUser::getUid, bo.getUid());
        lqw.eq(StringUtils.isNotBlank(bo.getAccessCode()), BlogSocialUser::getAccessCode, bo.getAccessCode());
        lqw.eq(StringUtils.isNotBlank(bo.getUnionId()), BlogSocialUser::getUnionId, bo.getUnionId());
        lqw.eq(StringUtils.isNotBlank(bo.getScope()), BlogSocialUser::getScope, bo.getScope());
        lqw.eq(StringUtils.isNotBlank(bo.getTokenType()), BlogSocialUser::getTokenType, bo.getTokenType());
        lqw.eq(StringUtils.isNotBlank(bo.getIdToken()), BlogSocialUser::getIdToken, bo.getIdToken());
        lqw.eq(StringUtils.isNotBlank(bo.getMacAlgorithm()), BlogSocialUser::getMacAlgorithm, bo.getMacAlgorithm());
        lqw.eq(StringUtils.isNotBlank(bo.getMacKey()), BlogSocialUser::getMacKey, bo.getMacKey());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), BlogSocialUser::getCode, bo.getCode());
        lqw.eq(StringUtils.isNotBlank(bo.getOauthToken()), BlogSocialUser::getOauthToken, bo.getOauthToken());
        lqw.eq(StringUtils.isNotBlank(bo.getOauthTokenSecret()), BlogSocialUser::getOauthTokenSecret, bo.getOauthTokenSecret());
        return lqw;
    }

    /**
     * 新增社会化用户
     */
    @Override
    public Boolean insertByBo(BlogSocialUserBo bo) {
        BlogSocialUser add = BeanUtil.toBean(bo, BlogSocialUser.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改社会化用户
     */
    @Override
    public Boolean updateByBo(BlogSocialUserBo bo) {
        BlogSocialUser update = BeanUtil.toBean(bo, BlogSocialUser.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BlogSocialUser entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除社会化用户
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 查询一个源和uuid
     *
     * @param source 源
     * @param uuid   uuid
     * @return {@link BlogSocialUserVo }
     * @author: Ann
     * @createDate: 2023/08/25 19:48:39
     * @version: 1.0.0
     */
    @Override
    public BlogSocialUserVo queryOneBySourceAndUuid(String source, String uuid) {
        return baseMapper.selectVoOne(new LambdaQueryWrapper<BlogSocialUser>()
            .eq(BlogSocialUser::getSource, source)
            .eq(BlogSocialUser::getUuid, uuid));
    }

    /**
     * 查询一个
     *
     * @param blogSocialUserBo 博客社会用户业务对象
     * @return {@link BlogSocialUserVo }
     * @author: Ann
     * @createDate: 2023/08/25 21:34:32
     * @version: 1.0.0
     */
    @Override
    public BlogSocialUserVo queryOne(BlogSocialUserBo blogSocialUserBo) {
        return baseMapper.selectVoOne(new LambdaQueryWrapper<BlogSocialUser>()
            .eq(BlogSocialUser::getSource, blogSocialUserBo.getSource())
            .eq(BlogSocialUser::getUuid, blogSocialUserBo.getUuid())
            .eq(BlogSocialUser::getAccessToken, blogSocialUserBo.getAccessToken()));
    }
}
